package com.wei.tang.util;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Random;

import org.json.JSONObject;

import com.wei.tang.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;

public class Util {

	/**
	 * 是否是第一次启动，若否则记录启动
	 */
	public static boolean isFirstTimeStart(Activity ac) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ac);
		boolean isFirstTime = prefs.getBoolean(R.string.project_name + "_first_time", true);
		if (isFirstTime) {
			SharedPreferences.Editor editor = prefs.edit();
			editor.putBoolean(R.string.project_name + "_first_time", false);// 输入数据"firstStart"值为false
			editor.commit();// 写入数据
		}
		return isFirstTime;
	}

	/**
	 * 通过文件名获取资源id 例子：getResId("icon", R.drawable.class);
	 *
	 * @param variableName
	 * @param c
	 * @return
	 */
	public static int getResId(String variableName, Class<?> c) {
		try {
			Field idField = c.getDeclaredField(variableName);
			return idField.getInt(idField);
		} catch (Exception e) {
			Tlog.e("getResId e:" + e.toString());
			return -1;
		}
	}

	public static String GetJosnString(JSONObject json, String josnStr) {
		String resultStr = "";
		try {
			if (!json.has(josnStr)) {
				return resultStr;
			}
			resultStr = json.getString(josnStr);
		} catch (Exception e) {
			Tlog.e("jamie", "JosnError: ---" + josnStr + "---" + e);
			resultStr = "";
		}
		return resultStr;
	}

	public static boolean GetJosnBoolean(JSONObject json, String josnStr) {
		boolean resultStr = false;
		try {
			if (!json.has(josnStr)) {
				return resultStr;
			}
			resultStr = json.getBoolean(josnStr);
		} catch (Exception e) {
			Tlog.e("jamie", "JosnError: ---" + josnStr + "---" + e);
			resultStr = false;
		}
		return resultStr;
	}

	public static double GetJosnDouble(JSONObject json, String josnStr) {
		double resultDouble = 0;
		try {
			if (!json.has(josnStr)) {
				return resultDouble;
			}
			if (json.getString(josnStr).equals("")) {
				resultDouble = 0;
				return resultDouble;
			}
			resultDouble = json.getDouble(josnStr);
		} catch (Exception e) {
			Tlog.e("jamie", "JosnError: ---" + josnStr + "---" + e);
			resultDouble = 0;
		}
		return resultDouble;
	}

	public static int GetJosnInt(JSONObject json, String josnStr) {
		int resultInt = 0;
		try {
			if (!json.has(josnStr)) {
				return resultInt;
			}
			if (json.getString(josnStr).equals("")) {
				resultInt = 0;
				return resultInt;
			}
			resultInt = json.getInt(josnStr);
		} catch (Exception e) {
			Tlog.e("jamie", "JosnError: ---" + josnStr + "---" + e);
			resultInt = 0;
		}
		return resultInt;
	}

	public static int GetJosnIntReturnFirst(JSONObject json, String josnStr) {
		int resultInt = 0;
		try {
			if (json.getString(josnStr).equals("")) {
				resultInt = 0;
				return resultInt;
			}
			resultInt = json.getInt(josnStr);
		} catch (Exception e) {
			Tlog.e("jamie", "JosnError: ---" + josnStr + "---" + e);
			resultInt = 1;
		}
		return resultInt;
	}

	/**
	 *  生成用户的登录是的session
	 * 
	 * @param phone
	 * @return
	 */
	public static String createUserSession(String phone) {
		Random rand = new Random();
		long number = Long.parseLong(phone);
		long time = new Date().getTime() * number;
		return time + "";
	}

	public static String getPath(Context context, Uri uri) {

		if ("content".equalsIgnoreCase(uri.getScheme())) {
			String[] projection = { "_data" };
			Cursor cursor = null;

			try {
				cursor = context.getContentResolver().query(uri, projection, null, null, null);
				int column_index = cursor.getColumnIndexOrThrow("_data");
				if (cursor.moveToFirst()) {
					return cursor.getString(column_index);
				}
			} catch (Exception e) {
				// Eat it
			}
		}

		else if ("file".equalsIgnoreCase(uri.getScheme())) {
			return uri.getPath();
		}

		return null;
	}

}
